package com.unicomcz.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.unicomcz.ActionBase;

public class ModPasAction extends ActionBase
{
    private String oldPas;
    private String passwd1;
    private String passwd2;
    private UserInfo userInfo;
    public String getOldPas()
    {
        return oldPas;
    }

    public void setOldPas(String oldPas)
    {
        this.oldPas = oldPas;
    }

    public String getPasswd1()
    {
        return passwd1;
    }

    public void setPasswd1(String paswd1)
    {
        this.passwd1 = paswd1;
    }

    public String getPasswd2()
    {
        return passwd2;
    }

    public void setPasswd2(String passwd2)
    {
        this.passwd2 = passwd2;
    }

    public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        log = Logger.getLogger(this.getClass());
        userInfo = (UserInfo)session.getAttribute("USER_INFO");
        if ((userInfo==null)||(!userInfo.isLogin())){
            //没有登陆
            log.debug("没有登陆！");
            return "login";
        }
        if ((passwd1 == null)||(passwd1.compareTo("")==0)) {
            addActionMessage("旧密码不能为空");
            return INPUT;
        }
        if (passwd1.compareTo(passwd2)!=0) {
            addActionMessage("两次输入的新密码不一致！");
            return INPUT;
        }
        if (oldPas.compareTo(userInfo.getPassword())!=0) {
            addActionMessage("旧密码输入错误！");
            return INPUT;
        }
        boolean dbFlag = setupConn("dls");
        if (!dbFlag) {
            log.debug(userInfo.getUserId()+"登陆系统失败!因为建立数据库连接失败");
            addActionMessage("修改密码失败!因为建立数据库连接失败");
            return INPUT;
        }
        //现在修改密码
        
        String sql = "update dls_code_dls set passwd=? where depart_id=?";
        int rtv = updateSingle(sql);
        if (rtv<=0) {
            //修改失败
            addActionMessage("修改密码失败!");
            return INPUT;
        }
        tearDown();
        addActionMessage("修改密码成功!");
        return SUCCESS;
    }
    protected void regParam2single() {
        try {
            pstmt.setString(1, passwd1);
            pstmt.setString(2, userInfo.getUserId());
            log.debug(userInfo.getUserId()+"修改密码到"+passwd1);
        }catch(Exception e) {
            
        }
    }
}
